<?php
if (isset($_SESSION['admin-login'])) {
	if (isset($param[2])) {
		if (isset($_SESSION['status-produk-edit'])) {
			echo "<p>".$_SESSION['status-produk-edit']."</p>";
			unset($_SESSION['status-produk-edit']);
		}
		$produk = db_result("SELECT * FROM produk WHERE id = ".intval($param[2]));
		?>
		<html><body>
		<img src="<?php echo site()."/asset/images/produk/".$produk['id'] ?>.jpg" width="150" /><br>
		<form enctype="multipart/form-data" action="<?php echo site()."/".$param[0]."/produk-edit-action/".$param[2] ?>" method="post" onsubmit="return validateProduk()">
			<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
			<input type="hidden" name="id" value="<?php echo $produk['id'] ?>" />
			Gambar, biarkan kosong jika tidak ingin diganti :<br><input name="image" type="file" /><br>
			Nama :<br><input type="text" name="nama" value="<?php echo $produk['nama'] ?>" /><br>
			Harga :<br>Rp <input type="text" name="harga" value="<?php echo $produk['harga'] ?>" /><br>
			Deskripsi :<br><textarea name="deskripsi"><?php echo $produk['deskripsi'] ?></textarea><br>
			Kategori :<br><input type="text" name="kategori" value="<?php echo $produk['kategori'] ?>" /><br>
			<input type="submit" name="post" id="post" value="Post" disabled="disabled" /><br>
			<a href="<?php echo site()."/".$param[0]."/produk" ?>">Cancel</a>
		</form>
		<script type="text/javascript">
		document.getElementById("post").removeAttribute("disabled");
		function validateProduk() {
			var bulat = /^\d+$/;
			var body = document.getElementsByTagName("body")[0];
			var form = document.getElementsByTagName("form")[0];
			var image = form["image"].value.split('.').pop().toLowerCase();
			var nama = form["nama"].value;
			var harga = form["harga"].value;
			var deskripsi = form["deskripsi"].value;
			var kategori = form["kategori"].value;
			var status = document.getElementsByTagName("p")[0] || document.createElement("p");
			if (nama.length == 0 || harga.length == 0 || deskripsi.length == 0 || kategori.length == 0) {
				status.innerHTML = "Semua input harus diisi";
				body.insertBefore(status,form);
			} else if (image.length != 0 && image != "jpg" && image != "jpeg") {
				status.innerHTML = "Tipe gambar hanya boleh jpg, atau jpeg";
				body.insertBefore(status,form);
			} else if (!bulat.test(harga)) {
				status.innerHTML = "Harga harus angka";
				body.insertBefore(status,form);
			} else {
				if (document.getElementsByTagName('p')[0])
					body.removeChild(document.getElementsByTagName('p')[0]);
				return true;
			}
			return false;
		}
		</script>
		</body></html>
		<?php
	}
} else {
	$_SESSION['status-login'] = "Log in terlebih dahulu untuk melanjutkan";
	header("Location: ".site()."/".$param[0]."/login");
}
?>